package com.xca.is.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xca.is.R;
import com.xca.is.databinding.ActivityCollegeBinding;
import com.xca.is.entity.CollegeEntity;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.CollegeModel;
import com.xca.is.utils.EditTextUtils;
import com.xca.is.utils.WebViewScriptUtils;

public class CollegeActivity extends AppCompatActivity implements HttpConnect {
    private ActivityCollegeBinding binding ;

    private ViewModelProvider provider ;
    private CollegeModel model ;
    private MutableLiveData<CollegeEntity> liveData ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCollegeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        int[] ints = new int[]{10,18};
        WebViewScriptUtils.htmlContent(ints,binding.collegeWeb);

        provider = new ViewModelProvider(this);
        model = provider.get(CollegeModel.class);
        model.setModel(this);
        liveData = (MutableLiveData<CollegeEntity>) model.getLiveData() ;
        liveData.observe(this, new Observer<CollegeEntity>() {
            @Override
            public void onChanged(CollegeEntity collegeEntity) {
                if (collegeEntity!=null){
                    showData(collegeEntity);
                }else {
                    binding.collegeEdit.setError("没有找到该学校");
                }
            }
        });

        getData();

    }


    private void getData(){
        binding.collegeLoading.rLoading.setVisibility(View.GONE);
        binding.c2.setVisibility(View.INVISIBLE);
        binding.collegeSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = binding.collegeEdit.getText().toString().trim() ;
                EditTextUtils.clearFocus(binding.collegeEdit);
                if (!name.isEmpty()){
                    model.getData(name);
                }else {
                    binding.collegeEdit.setError("请输入学校名称");
                }
            }
        });
    }
    private void showData(CollegeEntity collegeEntity){
        CollegeEntity.DataBean data = collegeEntity.getData() ;
        binding.college1.setText(data.getArea());
        binding.college2.setText(data.getFounding());
        binding.college3.setText(data.getAffiliate());
        binding.college4.setText(data.getEncode());
        binding.college5.setText(data.getDiscipline());
        binding.college6.setText(data.getLaboratory());
        binding.college7.setText(data.getDoctor());
        binding.college8.setText(data.getMaster());
        binding.college9.setText(data.getAddress());
        binding.college10.setText(data.getIntro());



WebSettings webSettings = binding.college11.getSettings();
webSettings.setJavaScriptEnabled(true);

        String detail = data.getDetail();
        String htmlContent = "<html lang=\"zh-CN\">\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            line-height: 1.6;\n" +
                "            margin: 10px;\n" +
                "            padding: 5px;\n" +
                "            background: transparent;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                detail +
                "</body>\n" +
                "</html>";
        binding.college11.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null);

    }

    @Override
    public void Loading(boolean loading) {
        binding.collegeLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
        binding.c2.setVisibility(loading ? View.INVISIBLE : View.VISIBLE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        binding.collegeLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}